<?php

namespace app\api\logic;

use app\common\model\Profession as ProfessionModel;
use app\common\model\ProfessionCategory;

class Profession
{
    /**
     * 考试关联分类
     */
    const PROFESSION_LAYER = [
        // 自考
        '1' => [
            [
                'id' => 1,
                'name' => '本科',
            ],
            [
                'id' => 2,
                'name' => '专科',
            ],
        ],

        // 成考
        '2' => [
            [
                'id' => 1,
                'name' => '专升本',
            ],
            [
                'id' => 2,
                'name' => '高升专',
            ],
            [
                'id' => 3,
                'name' => '高升本',
            ],
        ],
    ];

    /**
     * 层级字典
     */
    const LAYER_TO_OTHERS = [
        '1' => 76,
        '2' => 71,
        '3' => 96,
    ];

    /**
     * 查看专业列表
     * @param $param
     * @return array
     */
    public function getList($param): array
    {
        // 提取参数
        $exam_id = $param['exam_id'];
        $school_id = $param['school_id'] ?? 0;
        $province_id = $param['province_id'] ?? 0;

        // 提取分类
        $category = self::PROFESSION_LAYER[$exam_id] ?? [];

        // 提取新版分类
        $field = 'id,name';
        $where = [
            'exam_id' => $exam_id,
            'status' => 1,
        ];
        $newCategory = app(ProfessionCategory::class)->field($field)->where($where)->select();
        $newCategory = $newCategory ? $newCategory->toArray() : [];

        // 查询专业
        if ($exam_id == 1) {
            $where['province_id'] = $province_id;  // 自考需要查省份
        }
        if ($school_id) {
            $where['school_id'] = $school_id;
        }

        // 查询数据
        $list = app(ProfessionModel::class)->getList($where);

        // 返回
        return [
            'cate' => $category,
            'new_cate' => $newCategory,
            'list' => $list,
        ];
    }
}